package com.example.lab2;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.lab2.db.AppDBOpenHelper;
import com.example.lab2.db.Todolist;
import com.example.lab2.db.TodolistContract;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class ItemViewHolder extends RecyclerView.ViewHolder{
    private static final String TAG = "ItemViewHolder";
    public static Context baseContext;
    private int id;
    private CheckBox cb;
    private TextView txDes;
    private TextView txDate;
    private ImageButton btDelete;
    public ItemViewHolder(@NonNull View itemView) {
        super(itemView);
        cb=itemView.findViewById(R.id.checkBox);
        txDes=itemView.findViewById(R.id.text_item);
        txDate=itemView.findViewById(R.id.date_item);
        btDelete=itemView.findViewById(R.id.imageButton);
    }

    public void bind(Todolist todolist,TodolistAdapter parent){
        txDes.setText(todolist.getDescription());
        txDate.setText(todolist.getDate());
        if(todolist.getState()==0){
            txDes.getPaint().setFlags(0);
            cb.setChecked(false);
        }else {
            txDes.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            cb.setChecked(true);
        }

        cb.setOnClickListener(view -> {
            AppDBOpenHelper dbOpenHelper=new AppDBOpenHelper(baseContext);
            SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
            int newState=cb.isChecked()?1:0;
            Log.i(TAG, "bind: cb isChecked?"+cb.isChecked());
            ContentValues values=new ContentValues();
            values.put(TodolistContract.TodolistEntry.COL_NAME_STATE,newState);
            String selection= TodolistContract.TodolistEntry.COL_NAME_ID+" = ? ";
            String[] selectionArgs={""+todolist.getId()};
            int count=db.update(
                    TodolistContract.TodolistEntry.TABLENAME,
                    values,
                    selection,
                    selectionArgs
            );
            db.close();
            Log.i(TAG, "bind: update rows--"+count);
            parent.refresh();
        });
        btDelete.setOnClickListener(view -> {
            AppDBOpenHelper dbOpenHelper=new AppDBOpenHelper(baseContext);
            SQLiteDatabase db=dbOpenHelper.getWritableDatabase();
            String selection= TodolistContract.TodolistEntry.COL_NAME_ID+" = ? ";
            String[] selectionArgs={""+todolist.getId()};
            int deleteRows=db.delete(
                    TodolistContract.TodolistEntry.TABLENAME,
                    selection,
                    selectionArgs
            );
            db.close();
            Log.i(TAG, "bind: delete rows--"+deleteRows);
            parent.refresh();
        });
    }

}
